﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.Entity;
using ClassLibrary.DAO;

public partial class UserControl_ucPartsUsed : System.Web.UI.UserControl
{
    TblPartUsedDAO partUsedDAO = new TblPartUsedDAO(Global.CONNECTION_STRING);
    TblProductsDAO productsDao = new TblProductsDAO(Global.CONNECTION_STRING);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadpartUsed();
            LoadPartDescription();

            lb_Brand.Visible = false;
            lb_Cost.Visible = false;
            lb_Model.Visible = false;
            lb_ProductCode.Visible = false;
            lb_RRP.Visible = false;

        }
    }
    // Load Part Description
    private void LoadPartDescription()
    {
        //cbParDescription.DataSource = productsDao.GetProductsList(1);
        //cbParDescription.DataValueField = "ProductID";
        //cbParDescription.DataTextField = "ProductName";
        //cbParDescription.DataBind();

        //ListItem lstItem = new ListItem("- Select -", "");
        //cbParDescription.Items.Insert(0, lstItem);

    }
    private void loadpartUsed()
    {
        string id = Request.QueryString["id"];

        try
        {
            if (id != null)
            {
                grdPartsUsed.DataSource = partUsedDAO.GetPartUsed(Convert.ToInt32(id));
                grdPartsUsed.DataBind();
            }
            else
            {
                // txtpartDescription.Text = string.Empty;
                txtquantity.Text = string.Empty;
            }
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
    }


    protected void btnSave_Click(object sender, EventArgs e)
    {
        bool flag = false;
        try
        {
            TblPartUsed partUsed = new TblPartUsed();
            
            int id = Global.ConvertToInt(Request.QueryString["id"]);

            if (id != 0)
            {
                partUsed.ProductID = Global.ConvertToInt(cbParDescription.SelectedValue);
                partUsed.PhoneBrand = lbBrand.Text.Trim();
                partUsed.PhoneModel = lbModel.Text.Trim();
                partUsed.PartDescription = cbParDescription.SelectedItem.ToString() + " .Product code : " + lbProductCode.Text.Trim();
                partUsed.Quantity = Global.ConvertToInt(txtquantity.Text.Trim());
                partUsed.DateEntered = DateTime.Now;
                partUsed.JobID = id;

                partUsedDAO.InsUpPartUsed(partUsed, 1);

                flag = true;
            }
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
        if (flag)
           
            loadpartUsed();
    }


    protected void cbParDescription_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataTable dt = productsDao.GetPart_ByID(Global.ConvertToInt(cbParDescription.SelectedValue));
        foreach (DataRow dr in dt.Rows)
        {
            lbCost.Text = dr["Cost"].ToString();
            lbRRP.Text = dr["RRP"].ToString();
            lbModel.Text = dr["Model"].ToString();
            lbBrand.Text = dr["BrandName"].ToString();
            lbProductCode.Text = dr["productCode"].ToString();
        }
        lb_Brand.Visible = true;
        lb_Cost.Visible = true;
        lb_Model.Visible = true;
        lb_ProductCode.Visible = true;
        lb_RRP.Visible = true;
    }
    protected void txtSearchPart_TextChanged(object sender, EventArgs e)
    {
        cbParDescription.DataSource = productsDao.GetPartSearch(txtSearchPart.Text.Trim(),1);
        cbParDescription.DataValueField = "ProductID";
        cbParDescription.DataTextField = "ProductName";
        cbParDescription.DataBind();

        ListItem lstItem = new ListItem("- Select -", "");
        cbParDescription.Items.Insert(0, lstItem);

    }
}
